﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XBoshi.SecurityCommon;

namespace XBoshiGFS.forms
{
    public partial class LoginForm : Form
    {

        


        public LoginForm()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(TBCode.Text )) {
                MessageBox.Show("请填写登录账号"); 
                return;
            }
            if (String.IsNullOrWhiteSpace(TBPassword.Text  ))
            {
                MessageBox.Show("请填写登录密码");
                return;
            }
            String strUserCode = TBCode.Text;
            String strPassWord = TBPassword.Text; 

            using(var context =new XBoshiGFSModelContainer() ){


                var result =(from p in context.UsersSet where p.strCode ==strUserCode select p);


                if (result.Count() != 1) {
                    MessageBox.Show("用户不存在");
                    return;
                }
                var user = result.FirstOrDefault();
                if (user.strPassWord == strPassWord)
                {

                   MainMDIParent.currentUserCode = user.strCode;
                   MainMDIParent.station = user.StationSet; 
                   
                 //保存当前登录用户信息
                    DialogResult = DialogResult.OK; 
                    this.Close();
                }
                else {

                    MessageBox.Show("用户密码不正确");
                    return;
                }
            }
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {

            MachineNumber.ApplicationPath = Application.ExecutablePath;
            DateTime beginTime=new DateTime(2012,8,1);
            if (DateTime.Now >= beginTime)
            {
                LicenseClass checkResult = MachineNumber.CheckRegist(new LicenseClass());



                //检查版本、如果是使用版本，提示使用次数
                if (checkResult.isTempLincese == false)
                {
                    //正常注册用户
                }
                else if (checkResult.remainTimes <= 0)
                {
                    //使用次数到，不运行使用
                    MessageBox.Show(String.Format("试用用户，试用次数用完，请联系获取试用授权！", checkResult));
                    this.Close();
                }

                else if (checkResult.remainTimes > 30)
                {
                    //还有三十次使用机会，不提示
                }
                else if (checkResult.remainTimes <= 30)
                {
                    MessageBox.Show(String.Format("试用用户，还有{0}次使用机会！", checkResult));
                }

                MainMDIParent.license = checkResult;
            }
            else
            {
                 MainMDIParent.license = new LicenseClass();
            }

        }
    }
}
